const cateListTpl = require('../views/cate-list.html');
const navTpl = require('../views/nav.html');
const leftTpl = require('../views/cate-list-left.html');
const rightTpl = require('../views/cate-list-right.html');
$("#cate").html(cateListTpl);
$("#footer").html(navTpl);
const fetch = require('../models/fetch.js').default;
let dataArr = [];


fetch.jsonp("cateApi/cate/catelist?pf=8&area=4&bi=222&dtype=jsonp&_=1560045878699&callback=jsonp2", "cb")
      .then((res) => {
            dataArr = res;
            const renderLeftTpl = template.render(leftTpl, { res });
            $('#cate-list-left').html(renderLeftTpl);
            let data = res[0].secondCateList;
            renderRight(data,0) ;
            $('#cate-list-left').on("click", 'li', onCateClick);
      })
function onCateClick() {
      const id = $(this).attr("data-id");
      if (dataArr.length <= 0) return false;
      dataArr.some((elem, index) => {
            if (elem.id == id) {
                  console.log(1)
                  renderRight(elem.secondCateList,index);
                  return true;
            }
      })
}
function renderRight(data,index) {
      console.log(data,index)
      const renderRightTpl = template.render(rightTpl, { data });
      $('#cate-list-right').html(renderRightTpl);
      $('#cate-list-left li').eq(index).addClass('active').siblings().removeClass('active');
}
// async function getCateData(){
//     let dataJson=await fetch.getData('cateApi/cate/catelist?pf=8&area=4&bi=222&dtype=jsonp&_=1560045878699&callback=jsonp2');

//     let data=JSON.parse(dataJson);
//     console.log(data.responseText);
//     console.log(data);
// }
// getCateData();
// https://shop.juanpi.com/gsort?key=%7B%22cdt%22%3A%7B%22is_show_presale%22%3A%220%22%2C%22activity_type%22%3A%5B%221%22%2C%223%22%2C%225%22%2C%226%22%2C%2220%22%5D%2C%22hot_show_type%22%3A1%2C%22fcate%22%3A%222109%22%2C%22sale_type%22%3A2%7D%2C%22order%22%3A%7B%22show_stime%22%3A%22desc%22%2C%22sales%22%3A%22desc%22%2C%22fav%22%3A%22desc%22%2C%22sort%22%3A%22desc%22%2C%22cprice%22%3A%22desc%22%7D%7D&type=50&zhouyi_ids=p8_c4_l4&machining=danpin&page=2&rows=10&dtype=JSONP&price_range=&cat_threeids=&filter_id=&callback=gsort_callback